package com.ws.bean;

import java.util.Random;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.ws.helper.CasinoAppReturnObject;

/**
 * Session Bean implementation class CasinoAppBean
 * 
 * @author C707538
 */
@Stateless(name = "CasinoAppBean")
@Remote(CasinoAppInterface.class)
public class CasinoAppBean implements CasinoAppInterface {

	private final static int min = 1;

	private final static int amountOfPicturesOnWheel = 6;

	private int amountOfPlayers;

	private int winningNumber;

	private int playedRounds;

	/**
	 * Default constructor.
	 */
	public CasinoAppBean() {
		super();
	}

	public void initializeGame(int anAmountOfPlayers) {
		setAmountOfPlayers(anAmountOfPlayers);
		int randomNumber = getRandomNumberOfRange(anAmountOfPlayers);
		setWinningNumber(randomNumber);
		setPlayedRounds(0);
	}

	public CasinoAppReturnObject faitesLesJeux() {
		CasinoAppReturnObject retObject = new CasinoAppReturnObject();
		// just gamble if some players defined
		if (getAmountOfPlayers() > 0) {
			addPlayedRound();
			System.out.println("This is round number: " + getPlayedRounds());
			if (getPlayedRounds() == getWinningNumber()) {
				// Here we have a winner!
				retObject.setPlayerWins(true);
				int number = getRandomNumberOfRange(getAmountOfPlayers());
				retObject.setNumberRow1(number);
				retObject.setNumberRow2(number);
				retObject.setNumberRow3(number);
				System.out.println("This round wins the pot!");
			} else {
				// no winner; give different numbers
				retObject.setPlayerWins(false);
				int number1 = getRandomNumberOfRange(amountOfPicturesOnWheel);
				int number2 = getRandomNumberOfRange(amountOfPicturesOnWheel);
				int number3 = getRandomNumberOfRange(amountOfPicturesOnWheel);

				// check the 3 numbers; they shouldn't be same...
				if (number1 == number2 && number1 == number3) {
					// otherwise increase (or degrade) the number 2
					number3 = number3 < amountOfPicturesOnWheel ? number3++
							: number3--;
				}
				retObject.setNumberRow1(number1);
				retObject.setNumberRow2(number2);
				retObject.setNumberRow3(number3);
				System.out.println("Thanks for playing, try it an other day!");
			}
		} else {
			retObject.setPlayerWins(false);
			retObject.setNumberRow1(-1);
			retObject.setNumberRow2(-1);
			retObject.setNumberRow3(-1);
		}
		return retObject;
	}

	/**
	 * nextInt is normally exclusive of the top value, so add 1 to make it
	 * inclusive
	 * 
	 * @param anAmountOfPlayers
	 * @return a randomNumber
	 */
	private int getRandomNumberOfRange(int anAmountOfPlayers) {
		int randomNumber = new Random().nextInt((anAmountOfPlayers + 1) - min)
				+ min;
		return randomNumber;
	}

	private int getAmountOfPlayers() {
		return amountOfPlayers;
	}

	private void setAmountOfPlayers(int amountOfPlayers) {
		this.amountOfPlayers = amountOfPlayers;
	}

	private int getWinningNumber() {
		return winningNumber;
	}

	private void setWinningNumber(int winningNumber) {
		this.winningNumber = winningNumber;
	}

	private int getPlayedRounds() {
		return playedRounds;
	}

	private void setPlayedRounds(int playedRounds) {
		this.playedRounds = playedRounds;
	}

	private void addPlayedRound() {
		this.playedRounds++;
	}

}
